<?php
/**
 * @version     1.0.0
 * @package     com_events
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      JORGE EDUARDO DE O LIVEIRA LUCIANO <jeolcavaco@hotmail.com> - http://jorgeeduardo.wordpress.com
 */

// No direct access.
defined('_JEXEC') or die;

jimport('joomla.application.component.modelform');
jimport('joomla.event.dispatcher');

/**
 * Events model.
 */
class ganeshavereadoresModelProposicao  extends JModelForm
{
    
    var $_item = null;
    
	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState()
	{
		$app = JFactory::getApplication('com_ganeshavereadores');
		
		// Load state from the request userState on edit or from the passed variable on default
        if (JFactory::getApplication()->input->get('layout') == 'edit') {
            $id = JFactory::getApplication()->getUserState('com_ganeshavereadores.edit.proposicao.id');
        } else {
            $id = JFactory::getApplication()->input->get('id');
            JFactory::getApplication()->setUserState('com_ganeshavereadores.edit.proposicao.id', $id);
        }
		$this->setState('proposicao.id', $id);

		// Load the parameters.
		$params = $app->getParams();
        $params_array = $params->toArray();
        if(isset($params_array['item_id'])){
            $this->setState('proposicao.id', $params_array['item_id']);
        }
		$this->setState('params', $params);
		
	}
        

	/**
	 * Method to get an ojbect.
	 *
	 * @param	integer	The id of the object to get.
	 *
	 * @return	mixed	Object on success, false on failure.
	 */
	public function &getData($id = null)
	{
		
		if ($this->_item === null)
		{
			$this->_item = false;

			if (empty($id)) {
				$id = $this->getState('proposicao.id');
			}

			
			$db = $this->getDbo();
			$query = $db->getQuery(true);
			
			// Select the required fields from the table.
			$query->select(
					$this->getState(
							'list.select', 'a.*'
					)
			);
			
			$query->from('`#__ganeshavereadores_proposicoes` AS a');
			$query->select('ca.nome AS catalias');
			$query->join('LEFT', '#__ganeshavereadores_categoria AS ca ON ca.id=a.categoria');
			
			// Join over the users for the checked out user.
			$query->select('uc.name AS editor');
			$query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
			
			// Join over the created by field 'created_by'
			$query->select('created_by.name AS created_by');
			$query->join('LEFT', '#__users AS created_by ON created_by.id = a.created_by');
			$query->order('a.data DESC, a.numero');
			
			
			
			
				$query->where('( a.id = '.$id.' )');
			
			
			$db->setQuery($query); 
			
			$item = $db->loadObjectList();
			
			
			$this->_item = $item[0];
			
			
			
		}

		return $this->_item;
	}
    
	public function getTable($type = 'Proposicao', $prefix = 'ganeshavereadoresTable', $config = array())
	{   
	
        $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR.'/tables');
        return JTable::getInstance($type, $prefix, $config);
	}     
    	    
	/**
	 * Method to get the profile form.
	 *
	 * The base form is loaded from XML 
     * 
	 * @param	array	$data		An optional array of data for the form to interogate.
	 * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.
	 * @return	JForm	A JForm object on success, false on failure
	 * @since	1.6
	 */
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_ganeshavereadores.proposicao', 'proposicao', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return	mixed	The data for the form.
	 * @since	1.6
	 */
	protected function loadFormData()
	{
		$data = $this->getData(); 
        
        return $data;
	}    
}